var app = getApp(), api = getApp().api, _imgArray = [];

Page({
    data: {
        disabled: !1,
        mch: [],
    },

    onLoad: function (t) {
        getApp().page.onLoad(this, t);
        var a = this;
        app.request({
            url: api.mch.user.myshop,
            success: function(e) {
                a.setData({
                    mch: e.data.mch
                })
            }
        }), getApp().core.getLocation({
            type: 'wgs84',
            success: function(o) {
                console.log(o), app.core.setStorageSync("Location", o);
                var e = o.latitude + "," + o.longitude;
                app.request({
                    url: api.default.qq_map,
                    data: {
                        op: e
                    },
                    success: function(t) {
                        var city = t.data.result.address_component.city, district = t.data.result.address_component.district,
                            province = t.data.result.address_component.province;
                        console.log(t), a.setData({
                            cityname: district,
                            address: t.data.result.address,
                            lat: o.latitude,
                            lng: o.longitude,
                        }); 
                    }
                })
            }
        })
    },

    imgArray1: function() {
        var a = this, t = 9 - _imgArray.length;
        0 < t && t <= 9 ?  getApp().core.chooseImage({
            count: t,
            sizeType: [ "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(e) {
                app.core.showToast({
                    icon: "loading",
                    title: "正在上传"
                });
                var t = e.tempFilePaths;
                a.uploadimg({
                    url: api.default.upload_image,
                    path: t
                });
            }
        }) : getApp().core.showModal({
            title: "上传提示",
            content: "最多上传9张图片",
            confirmText: "确定"
        })
    },
    uploadimg: function(e) {
        var t = this, a = e.i ? e.i : 0, n = e.success ? e.success : 0, i = e.fail ? e.fail : 0;
        app.core.uploadFile({
            url: e.url,
            filePath: e.path[a],
            name: "image",
            formData: null,
            success: function(e) {
                var d = JSON.parse(e.data);
                console.log(d), 0 == d.code ? (n++, _imgArray.push(d.data.url), t.setData({
                    imgArray1: _imgArray
                })) : app.core.showToast({
                    icon: "none",
                    title: "您所选的第" + (a + 1) + "张图片内容违规，系统已过滤"
                }); 
            },
            fail: function(e) {
                i++;
            },
            complete: function() {
                ++a == e.path.length || (e.i = a, e.success = n, e.fail = i, t.uploadimg(e));
            }
        });
    },
    delete: function(e) {
        Array.prototype.indexOf = function(e) {
            for (var t = 0; t < this.length; t++) if (this[t] == e) return t;
            return -1;
        }, Array.prototype.remove = function(e) {
            var t = this.indexOf(e);
            -1 < t && this.splice(t, 1);
        };
        var t = e.currentTarget.dataset.inde;
        _imgArray.remove(_imgArray[t]), this.setData({
            imgArray1: _imgArray
        });
    },
    formSubmit: function(e) {
        console.log(e);
        var t = this, y = (e.detail.formId, e.detail.value.content.replace("\n", "↵")), g = e.detail.value.name, f = e.detail.value.tel, o = e.detail.value.dzaddress, c = e.detail.value.cityname, k = "";
        if ("" == y ? k = "内容不能为空" : "" == g ? K = "姓名不能为空" : "" == f ? k = "电话不能为空" : "" != k) app.core.showModal({
            title: "提示",
            content: k,
        }); else {
            if (0 == _imgArray.length) var L = ""; else L = _imgArray.join(",");
            t.setData({
                disabled: !0
            }), app.request({
                url: api.mch.article.post,
                method: "post",
                data: {
                    img: L,
                    content: y,
                    mch_name: g,
                    mch_tel: f,
                    address: o,
                    cityname: c,
                    lat: t.data.lat,
                    lng: t.data.lng,
                },
                success: function(e) {
                    0 == e.data.code ? (wx.showToast({
                        title: "发布成功",
                        mask: !0
                    }), setTimeout(function() {
                        wx.reLaunch({
                            url: "../article-list/article-list"
                        });
                    }, 1e3)) : (t.setData({
                        disabled: !0
                    }), wx.showToast({
                        icon: "none",
                        title: e.data.msg
                    }));
                }
            })
        }

    },

    address: function(e) {
        var a = this;
        getApp().core.chooseLocation({
            type: "wgs84",
            success: function(e) {
                console.log(e);
                e.latitude, e.longitude, e.speed, e.accuracy;
                var t = e.latitude + "," + e.longitude;
                app.request({
                    url: api.default.qq_map,
                    data: {
                        op: t
                    },
                    success: function(i) {
                        var city = i.data.result.address_component.city, district = i.data.result.address_component.district,
                        province = i.data.result.address_component.province;
                        console.log(i), a.setData({
                            address: e.address,
                            cityname: district,
                            lat: e.latitude,
                            lng: e.longitude,
                        })
                    }
                });
            }
        });
    },

    onReady: function () {},
    onShow: function () {},
    onHide: function () {},
    onUnload: function () {},
    onPullDownRefresh: function () { },
    onReachBottom: function () { },
    onShareAppMessage: function () {}
})